package com.globalresources.struts.app;
import java.util.Date;
import java.util.List;

import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;

import com.globalresources.business.inface.IUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Constant;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerService;
import com.globalresources.po.User;


/**
 * @author songduanlei@gmail.com
 * @since 创建时间：2009-7-24
 */

public class UserAction extends AbstractAction {
	private List<User> users;
	private Pager pager;	
	private PagerService pagerService;
	private IUserService userService;
	private String currentPage;
	private String pagerMethod;
	private String totalRow;	
	private User user;
	
	private String fmessage;
	
	private String oldPassword;
	private String newPassword;
	private String newPassword2;	
	private String username;

	/***
	 * @author songduanlei@gmail.com
	 * 查看个人信息
	 * @return
	 */
	public String getUserInfo(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails ud = (UserDetails)obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User)list.get(0);				
			}
		}		
		System.out.println(user.getUsername()+user.getDetailUser().getFamilyName());
		setFmessage("Edit Save Success");
		return SUCCESS;
	}
	/***
	 * @author songduanlei@gmail.com
	 * 修改个人信息
	 * @return
	 */
	public String editUserInfo(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails ud = (UserDetails)obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User)list.get(0);				
			}
		}		
		return SUCCESS;
	}
	
	
	/***
	 * @author songduanlei@gmail.com
	 * 保存个人信息
	 * @return
	 */
	public String saveUserInfo(){

			if (null != user) {
				userService.saveUser(user);	
			}
			return SUCCESS;
		}
	
	/***
	 * @author songduanlei@gmail.com
	 * Change User Password
	 * @return
	 */
	public String editPassword() {

		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		if (null != obj && obj instanceof UserDetails) {
			UserDetails ud = (UserDetails) obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User) list.get(0);
				String password = user.getPassword();
				if (!password.equals(oldPassword)) {
					setFmessage("oldPassword is not ture");
					return ERROR;
				} else {
					if (!newPassword.equals(newPassword2)) {
						setFmessage("newPassword is not equal to the confirm new Password");
						return ERROR;
					} else {

						user.setPassword(newPassword);
						userService.saveOrUpdateUser(user);
						setFmessage("change password success");
						return SUCCESS;

					  }
				    }
			}else{
				return ERROR;
			}
		}else{
			return ERROR;
		}

	}
	
	public String index(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (obj instanceof UserDetails) {
			UserDetails userDetails = (UserDetails)obj;
			setUsername(userDetails.getUsername());
		}		
		return SUCCESS;
	}
	/***************************************************************************
	 * @author songduanlei@gmail.com 返回所有用户信息
	 * @return
	 * 
	 * public String list(){ int _totalRow = userService.countUsers(pusername);
	 * pager = pagerService.getPager(currentPage, pagerMethod, _totalRow);
	 * setCurrentPage(String.valueOf(pager.getCurrentPage()));
	 * setTotalRow(String.valueOf(_totalRow)); users =
	 * userService.getUsers(pusername); return SUCCESS; }
	 * 
	 */
	
	
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public PagerService getPagerService() {
		return pagerService;
	}
	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public String getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	public String getPagerMethod() {
		return pagerMethod;
	}
	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}
	public String getTotalRow() {
		return totalRow;
	}
	public void setTotalRow(String totalRow) {
		this.totalRow = totalRow;
	}


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}
	/**
	 * @return the fmessage
	 */
	public String getFmessage() {
		return fmessage;
	}
	/**设置传递过去的参数
	 * @param fmessage the fmessage to set
	 */
	public void setFmessage(String fmessage) {
		this.fmessage = fmessage;
	}
	/**
	 * @return the oldPassword
	 */
	public String getOldPassword() {
		return oldPassword;
	}
	/**
	 * @param oldPassword the oldPassword to set
	 */
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	/**
	 * @return the newPassword
	 */
	public String getNewPassword() {
		return newPassword;
	}
	/**
	 * @param newPassword the newPassword to set
	 */
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	/**
	 * @return the newPassword2
	 */
	public String getNewPassword2() {
		return newPassword2;
	}
	/**
	 * @param newPassword2 the newPassword2 to set
	 */
	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	
}